package cn.sinjet.communication.broadcast;

import com.mgroup.wazehelper.R;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import cn.sinjet.carassist.SinjetApplication;
import cn.sinjet.viewmodel.ViewModel;

public class SinjetBroadcastHandler extends BroadcastReceiver{
	public static final String ACTION_MEDIA_FOCUS_STATE = "cn.sinjet.action.MEDIA_FOCUS_STATE";
	public static final String ACTION_MEDIA_PLAY_PAUSE = "cn.sinjet.action.MEDIA_PLAY_PAUSE";
	public static final String ACTION_MEDIA_PLAY = "cn.sinjet.action.MEDIA_PLAY";
	public static final String ACTION_MEDIA_PAUSE = "cn.sinjet.action.MEDIA_PAUSE";
	public static final String ACTION_MEDIA_NEXT = "cn.sinjet.action.MEDIA_NEXT";
	public static final String ACTION_MEDIA_PREV = "cn.sinjet.action.MEDIA_PREV";
	public static final String ACTION_TRACK_DETAILS = "cn.sinjet.action.MEDIA_TRACK_DETAILS";
	Context context;
	public SinjetBroadcastHandler(Context context){
		this.context  = context;
	}
	public void initReceiver(){
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_MEDIA_FOCUS_STATE);
		filter.addAction(ACTION_TRACK_DETAILS);
		context.registerReceiver(this, filter);
	}
	public void uninitReceiver(){
		context.unregisterReceiver(this);
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String action = intent.getAction().toString();
	   Log.d("broadcast", "onRecv:"+action);
	   if(action.equals(ACTION_MEDIA_FOCUS_STATE)){
		   int focuseState = intent.getIntExtra("MEDIA_FOCUS_STATE", 0);
		   Log.d("broadcast", "audiofocus:"+focuseState);
		   SinjetApplication.getInstance().onMyMediaFocusSate(focuseState);
	   }else if(action.equals(ACTION_TRACK_DETAILS)){
		   String title = intent.getStringExtra("title");
		   String album = intent.getStringExtra("album");
		   String artist = intent.getStringExtra("artist");
		   Log.d("broadcast", "mediainfo:"+title+" "+album+" "+artist);
		   onRecvMediaInfo(title,album,artist);
	   }
	}

	private void onRecvMediaInfo(String title, String album, String artist) {
		// TODO Auto-generated method stub
		if(title != null && !title.isEmpty())
		    ViewModel.getIns().setViewProperty(R.id.home_music_title, title);
		else
			ViewModel.getIns().setViewProperty(R.id.home_music_title, (String)"");
		if(artist != null && !artist.isEmpty())
		    ViewModel.getIns().setViewProperty(R.id.home_music_artist, artist);
		else if(album != null && !album.isEmpty())
			ViewModel.getIns().setViewProperty(R.id.home_music_artist, album);
	}
	public static void requestPlayNext(Context context){
		Intent intent = new Intent(ACTION_MEDIA_NEXT);
	    context.sendBroadcast(intent);
	}
	public static void requestPlayPause(Context context){
		Intent intent = new Intent(ACTION_MEDIA_PLAY_PAUSE);
	    context.sendBroadcast(intent);
	}
	public static void requestPlayPrev(Context context){
		Intent intent = new Intent(ACTION_MEDIA_PREV);
	    context.sendBroadcast(intent);
	}
	public static void requestPlay(Context context){
		Intent intent = new Intent(ACTION_MEDIA_PLAY_PAUSE);
	    context.sendBroadcast(intent);
	}
	public static void requestPause(Context context){
		Intent intent = new Intent(ACTION_MEDIA_PLAY_PAUSE);
	    context.sendBroadcast(intent);
	}
}
